Skip to main content

Cerate a Server (Windows)

Import necessary packages.​

To create a website interface, we can use Flask to create a web application. It is a lightweight and flexible web framework for Python. Find more information about Flask here.

from flask import Flask, Response, jsonify, render_template

In this snippet:

  • Flask: imports the Flask class from the flask module.
  • Response: represents the data sent back to the client (typically a browser) following a request.
  • jsonify: converts a Python dictionary or list into a JSON string.
  • render_template: renders a template from the template folder with the provided context.

Last, import other custom modules we create and use sys.path.append to add the path of the module.

import sys
sys.path.append('02 Code/01 Python/02 Camera/01 Gestures')
from hand_track import *
from robot_behaviour import *

Create Flask instance​

app = Flask(__name__)

__name__: it is a special variable in Python that is used to determine whether the script is being run on its own or being imported. If the script is being run on its own, __name__ is set to __main__. If the script is being imported, name is set to the name of the script.

Create Hand Track instance​

To access all the functions in the hand_track module, we need to create an instance of the HandTrack class.

my_hand_track = HandTrack()

Setup a route​

To send output to the client, we can use the route() decorator to tell Flask what URL should trigger what function.

@app.route('/your_URL')
def function():
return output

In this snippet:

  • @app.route(): is a decorator that creates a mapping between the URL and the function.
  • '/your_URL': defines the URL that should trigger the function.
  • def function(): is the function that will be executed when the URL is accessed.
  • return output: returns the value to the client.

For example:

@app.route('/camera_feed')
def hand_track():
return Response(my_hand_track.run(), mimetype='multipart/x-mixed-replace; boundary=frame')

In this snippet, a route is created for the '/camera_feed' URL. When the URL is accessed, the hand_track() function is executed, which returns the output of the my_hand_track.run() defined before. The output is returned as a Response object with the specified mimetype.

@app.route('/data_send')
def hand_data():
data = my_hand_track.category_log
value = list(data.values())
if len(value) > 0:
ca_name = value[-1]
return jsonify({'category': ca_name})
else:
return jsonify({'category': 'Move your hand!'})

In this snippet, a route is created for the '/data_send' URL. When the URL is accessed, it will return the last value of my_hand_track.category_log and convert it to a JSON object using the jsonify() function.

Redner a template​

To embed the output of a route in a web page, we need a web page and use the render_template() function to render a template with the provided context.

@app.route('/')
def index():
return render_template('index.html')

Run the server​

To run the server, we can use the run() method of the Flask instance.

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
  • host='0.0.0.0': makes the server accessible from any network interface
  • port=5000: specifies the port number on which the server will run.
  • app.run(): Flask starts the development server, making your web application accessible at http://0.0.0.0:5000/ by default.